<?php
namespace App\Shop\Logic;

use App\Models\SwtGood;
use App\Models\SwtMachine;
use App\Models\SwtMerchantProfit;
use App\Models\SwtOrder;
use App\Models\SwtOrderGoods;
use App\Models\SwtOrderRefund;
use Illuminate\Support\Facades\Storage;

class OrderLogic extends BaseLogic
{


    /**
     * @notes 获取订单信息
     * @param $order_id
     * @return array|false
     * @author Evil丿
     * @date 2024/07/30 10:25
     */
    public static function getGoodsList($order_id)
    {
        $goods_list = SwtOrderGoods::where('order_id',$order_id) -> get();

        if(!$goods_list) {
            self::$error = "商品信息不存在";
            return false;
        }

        $goods = [];

        foreach($goods_list as $v) {
            $goods_picture = SwtGood::where('id', $v -> goods_id) -> value('goods_picture');

            $goods[] = [
                'goods_picture' => $goods_picture ? Storage::url($goods_picture) : '',
                'goods_name'    => $v -> goods_name,
                'goods_num'     => $v -> goods_num,
                'goods_price'   => (float) bcdiv($v -> goods_price, 100, 2)
            ];
        }
        return $goods;
    }

    /**
     * @notes 获取订单列表
     * @param $user_id
     * @param $data
     * @return array
     * @author Evil丿
     * @date 2024/08/05 14:59
     */
    public static function getOrderList($params)
    {
        $page_no = 1;

        if(key_exists('page_no', $params) && $params['page_no']) {
            $page_no = $params['page_no'];
        }

        $page_size = 10;

        if(key_exists('page_size', $params) && $params['page_size']) {
            $page_size = $params['page_size'];
        }


        $where = function($q) use ($params) {
            if(key_exists('start_time', $params) && $params['start_time'] && key_exists('end_time', $params) && $params['end_time']) {
                $q -> whereBetween('swt_merchant_profit.created_at', [$params['start_time'], $params['end_time']]);
            }
            if(key_exists('device_id', $params) && $params['device_id']) {
                $q -> where('swt_order.machine_id', $params['device_id']);
            }

            $q -> where('swt_merchant_profit.merchant_id', $params['merchant_id']);
        };

        $offset = ($page_no - 1) * $page_size;

        $list = SwtMerchantProfit::join('swt_order','swt_merchant_profit.order_id','=','swt_order.id','right')
            -> where($where)
            -> orderBy('swt_merchant_profit.created_at','desc')
            -> limit($page_size)
            -> offset($offset)
            -> get();

        $count = SwtMerchantProfit::join('swt_order','swt_merchant_profit.order_id','=','swt_order.id','right')
            -> where($where)-> count();

        $sale_amount_add =  SwtMerchantProfit::join('swt_order','swt_merchant_profit.order_id','=','swt_order.id','right')
            -> where($where)
            -> where('swt_merchant_profit.type', 0)
            -> sum('swt_merchant_profit.amount');

        $sale_amount_sub =  SwtMerchantProfit::join('swt_order','swt_merchant_profit.order_id','=','swt_order.id','right')
            -> where($where)
            -> where('swt_merchant_profit.type', 1)
            -> sum('swt_merchant_profit.amount');

        $sale_profit_add =  SwtMerchantProfit::join('swt_order','swt_merchant_profit.order_id','=','swt_order.id','right')
            -> where($where)
            -> where('swt_merchant_profit.type', 0)
            -> sum('swt_merchant_profit.profit');

        $sale_profit_sub =  SwtMerchantProfit::join('swt_order','swt_merchant_profit.order_id','=','swt_order.id','right')
            -> where($where)
            -> where('swt_merchant_profit.type', 1)
            -> sum('swt_merchant_profit.profit');

        $arr = [];
        foreach ($list as $item)
        {
            $machine_sn = SwtMachine::where('id', $item -> machine_id) -> first();
            $arr[] = [
                'id'                => $item -> id,
                'order_sn'          => $item -> created_at -> format('YmdHis') . $item -> id,
                'machine_sn'        => $machine_sn -> machine_name,
                'machine_name'      => $machine_sn -> machine_name,
                'machine_address'   => $machine_sn -> address,
                'amount'            => (float) bcdiv($item -> amount, 100, 2),
                'profit'            => (float) bcdiv($item -> profit, 100, 2),
                'create_time'       => $item -> created_at -> format('Y-m-d H:i:s'),
                'payment_time'      => $item -> payment_at,
                'change_type'       => $item -> type == 0 ? 1 : 2,
                'avatar'            => Storage::disk('public')->url('images/logo.png'),
                'order_amount'      => (float) bcdiv($item -> order_amount, 100, 2),
                'pay_amount'        => (float) bcdiv($item -> pay_amount, 100, 2),
                'goodsList'         => self::getGoodsList($item -> id)
            ];
        }

        $data = [
            'list'          => $arr,
            'page_no'       => $page_no,
            'page_size'     => $page_size,
            'count'         => $count,
            'amount'        => (float) bcdiv(bcsub($sale_amount_add, $sale_amount_sub), 100, 2),
            'profit'        => (float) bcdiv(bcsub($sale_profit_add, $sale_profit_sub), 100, 2),
            'more'          => count($list) >= $page_size
        ];

        return $data;
    }

    public static function getOrderDetail($params)
    {
        $order_id = $params['order_id'];

        $order_info = SwtOrder::with(['goods','machine']) -> firstWhere('id', $order_id);

        if(!$order_info) {
            self::$error = "订单信息不存在";
            return false;
        }

        $status = 0;

        if($order_info -> is_pay) {
            $status = 1;
        }

        $refund_amount = (float) bcdiv($order_info -> refund_amount, 100 ,2);

        if($refund_amount > 0) {
            $refund_amount = (float) bcdiv(bcsub($order_info -> pay_amount, $order_info -> refund_amount),100, 2);
            $status = 3;
        }

        if($order_info -> is_refund) {
            $status = 2;
        }

        $order_refund_info = SwtOrderRefund::where('order_id', $order_info -> id) -> where('status', 0) -> first();

        if($order_refund_info) {
            $status = 4;
        }

        $goods_list = [];

        foreach($order_info -> goods as $v) {
            $goods_picture = SwtGood::where('id', $v -> goods_id) -> value('goods_picture');

            if(!$goods_picture) {
                $goods_picture = "/images/logo.png";
            }

            $goods_list[] = [
                'goods_picture' => Storage::url($goods_picture),
                'goods_name'    => $v -> goods_name,
                'goods_num'     => $v -> goods_num,
                'goods_price'   => (float) bcdiv($v -> goods_price, 100, 2)
            ];
        }

        $order_detail = [
            'id'                => $order_info -> id,
            'status'            => $status,
            'pay_amount'        => (float) bcdiv($order_info -> pay_amount, 100, 2),
            'order_amount'      => (float) bcdiv($order_info -> order_amount, 100, 2),
            'refund_amount'     => $refund_amount,
            'order_sn'          => $order_info -> created_at -> format('YmdHis') . $order_info -> id,
            'create_time'       => $order_info -> created_at -> format('Y-m-d H:i:s'),
            'payment_time'      => $order_info -> payment_at,
            'machine_name'      => $order_info -> machine -> nickname,
            'machine_address'   => $order_info -> machine -> address,
            'goods_list'        => $goods_list
        ];

        return $order_detail;
    }
}
